﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcApplication5.DAM;
using System.Data;
using MyFrameWork.Dam;
using MvcApplication5.Models;
using MyFrameWork.Common;
using MvcApplication5.Common;

namespace MvcApplication5.Controllers
{
    public class MasterController : MyController
    {
        #region Employee 处理
        //
        // GET: /Master/
        public ActionResult EmployeeList()
        {
            return View();
        }

        /// <summary>
        /// 员工信息取得
        /// </summary>
        /// <returns></returns>
        public ActionResult GetEmployeeList()
        {
            try
            {
                ListCondition listCondition = CommonUtility.GetListCondition(Request.Form);

                Pager pager = listCondition.ListPager;

                Dictionary<String, String> searchCondition = listCondition.SearchCondition;

                EmployeeDAM masterDAM = new EmployeeDAM();

                DataSet ds = masterDAM.GetEmployeeList(searchCondition, pager);

                List<UserModelExt> userModel = DataConvert<UserModelExt>.ConvertModelToDataSet(ds);

                return Content(JsonUtility.CreateSearchResultJson(pager.GetJson(), JsonUtility.ToJSON(userModel)));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 员工信息保存
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public ActionResult SaveEmployeeInfo()
        {
            int result;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            EmployeeDAM masterDAM = new EmployeeDAM();

            UserModel userModel = JsonUtility.Parse<UserModel>(CommonUtility.GetAjaxDataString());

            if (userModel.employee_id > 0)
            {
                //修改员工信息
                result = masterDAM.UpdateEmployeeInfo(userModel);
            }
            else
            {
                //添加员工信息
                result = masterDAM.SaveEmployeeInfo(userModel);
            }
            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "保存成功！";
            }
            else
            {
                ajaxResult.message = "保存出错！";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));

        }

        /// <summary>
        /// 验证code是否被占用
        /// </summary>
        /// <param name="user_cd">会员名称</param>
        /// <returns>查寻行数</returns>
        public ActionResult CheckEmployeeCd(string employee_code, string employee_id)
        {
            EmployeeDAM employeeDAM = new EmployeeDAM();
            string result = employeeDAM.CheckEmployeeCd(employee_code, employee_id).ToString();
            return Content(result);
        }
        /// <summary>
        /// 禁用员工帐号
        /// </summary>
        /// <param name="employee_id"></param>
        /// <returns></returns>
        public ActionResult DeleteEmployeeInfo()
        {
            List<string> employee_ids = Request.Form.GetValues("listKey[]").ToList();

            int result = 0;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            EmployeeDAM masterDAM = new EmployeeDAM();

            result = masterDAM.DeleteEmployeeInfo(employee_ids);

            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "Success";
            }
            else
            {
                ajaxResult.message = "Fail";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));
        }

        /// <summary>
        /// 取得权限数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRoleList()
        {
            try
            {
                DataSet roleDs = new EmployeeDAM().GetRoleList();

                List<RoleModel> roleModel = DataConvert<RoleModel>.ConvertModelToDataSet(roleDs);

                return Content(JsonUtility.ToJSON(roleModel));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取要修改的员工信息
        /// </summary>
        /// <param name="employee_id"></param>
        /// <returns></returns>
        public ActionResult GetEmployeeInfo()
        {
            EmployeeDAM employeeDAM = new EmployeeDAM();

            UserModel userModel = JsonUtility.Parse<UserModel>(CommonUtility.GetAjaxDataString());

            DataSet ds = employeeDAM.GetEmployeeInfo(userModel.employee_id.ToString());

            List<UserModel> userModelExt = DataConvert<UserModel>.ConvertModelToDataSet(ds);

            return Content(JsonUtility.ToJSON(userModelExt));
        }
        #endregion


        /// <summary>
        /// 取得职位数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPositionList()
        {
            try
            {
                DataSet positionDs = new EmployeeDAM().GetPositionList();

                List<PositionModelExt> positionModel = DataConvert<PositionModelExt>.ConvertModelToDataSet(positionDs);

                return Content(JsonUtility.ToJSON(positionModel));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #region Staff 处理
        //
        // GET: /Master/
        public ActionResult StaffList()
        {
            return View();
        }
        /// <summary>
        /// 根据position_id查找相应的员工
        /// </summary>
        /// <returns></returns>
        public ActionResult GetStaffListByPositionId()
        {
            try
            {
                //获取js传过来的条件以及pager的信息
                ListCondition listCondition = CommonUtility.GetListCondition(Request.Form);

                //转换成pager对象
                Pager pager = listCondition.ListPager;

                //获取js传来的查询条件
                Dictionary<String, String> searchCondition = listCondition.SearchCondition;

                //实例化staffDAM对象
                StaffDAM staffDAM = new StaffDAM();

                //调用staffDAM的实现方法
                DataSet ds = staffDAM.GetStaffListByPositionId(searchCondition, pager);

                //把DataSet的数据转换成model
                List<StaffModel> staffModel = DataConvert<StaffModel>.ConvertModelToDataSet(ds);

                //返回pager的信息和staffModel
                return Content(JsonUtility.CreateSearchResultJson(pager.GetJson(), JsonUtility.ToJSON(staffModel)));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取要修改的员工信息
        /// </summary>
        /// <param name="employee_id"></param>
        /// <returns></returns>
        public ActionResult GetStaffInfoById()
        {
            StaffDAM staffDAM = new StaffDAM();

            StaffModel staffModel = JsonUtility.Parse<StaffModel>(CommonUtility.GetAjaxDataString());

            DataSet ds = staffDAM.GetStaffInfoById(staffModel.staff_id.ToString());

            List<StaffModel> userModel = DataConvert<StaffModel>.ConvertModelToDataSet(ds);

            return Content(JsonUtility.ToJSON(userModel));
        }
        /// <summary>
        /// 员工信息保存
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        public ActionResult SaveStaffInfo()
        {
            int result;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            StaffModel staffModel = JsonUtility.Parse<StaffModel>(CommonUtility.GetAjaxDataString());

            StaffDAM masterDAM = new StaffDAM();

            if (staffModel.staff_id > 0)
            {
                //修改员工信息
                result = masterDAM.UpdateStaffInfo(staffModel);
            }
            else
            {
                //添加员工信息
                result = masterDAM.SaveStaffInfo(staffModel);
            }
            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "保存成功！";
            }
            else
            {
                ajaxResult.message = "保存出错！";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));

        }
        /// <summary>
        /// 禁用员工帐号
        /// </summary>
        /// <param name="employee_id"></param>
        /// <returns></returns>
        public ActionResult DeleteStaffInfo()
        {
            List<string> staff_ids = Request.Form.GetValues("listKey[]").ToList();

            int result = 0;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            StaffDAM masterDAM = new StaffDAM();

            result = masterDAM.DeleteStaffInfo(staff_ids);

            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "删除成功！";
            }
            else
            {
                ajaxResult.message = "删除失败！";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));
        }
        #endregion


        #region Position 处理
        /// <summary>
        /// 添加职位
        /// </summary>
        /// <param name="positionModel"></param>
        /// <returns></returns>
        public ActionResult SavePositionInfo()
        {
            int result;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            PositionModel positionModel = JsonUtility.Parse<PositionModel>(CommonUtility.GetAjaxDataString());

            PositionDAM masterDAM = new PositionDAM();

            if (positionModel.position_id > 0)
            {
                //修改职位
                result = masterDAM.UpdatePositionInfo(positionModel);
            }
            else
            {
                //添加职位
                result = masterDAM.SavePositionInfo(positionModel);

            }
            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "保存成功！";
            }
            else
            {
                ajaxResult.message = "保存出错！";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));

        }
        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult DeletePositionInfo()
        {
            int result = 0;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            PositionModel positionModel = JsonUtility.Parse<PositionModel>(CommonUtility.GetAjaxDataString());

            PositionDAM masterDAM = new PositionDAM();

            result = masterDAM.DeletePositionInfo(positionModel.position_id.ToString());

            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "Success";
            }
            else
            {
                ajaxResult.message = "Fail";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));
        }
        /// <summary>
        /// 获取职位表中的相应id的数据
        /// </summary>
        /// <param name="position_id"></param>
        /// <returns></returns>
        public ActionResult GetPositionInfo()
        {
            PositionDAM positionDAM = new PositionDAM();

            PositionModel positionModel = JsonUtility.Parse<PositionModel>(CommonUtility.GetAjaxDataString());

            DataSet ds = positionDAM.GetPositionInfo(positionModel.position_id.ToString());

            List<PositionModel> userModel = DataConvert<PositionModel>.ConvertModelToDataSet(ds);

            return Content(JsonUtility.ToJSON(userModel));
        }
        #endregion


        #region 权限与职位
        //
        // GET: /Master/
        public ActionResult RolePositionList()
        {
            return View();
        }

        /// <summary>
        /// 获取要修改的角色信息
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns></returns>
        public ActionResult GetRoleInfoByRoleId()
        {
            RolePositionDAM rolePositionDAM = new RolePositionDAM();

            RoleModel roleModel = JsonUtility.Parse<RoleModel>(CommonUtility.GetAjaxDataString());

            DataSet ds = rolePositionDAM.GetRoleInfoByRoleId(roleModel.role_id.ToString());

            List<RoleModel> userModel = DataConvert<RoleModel>.ConvertModelToDataSet(ds);

            return Content(JsonUtility.ToJSON(userModel));
        }
        /// <summary>
        /// 添加角色和修改角色
        /// </summary>
        /// <param name="positionModel"></param>
        /// <returns></returns>
        public ActionResult SaveRoleInfo()
        {
            int result;
            AjaxResultModel ajaxResult = new AjaxResultModel();

            RoleModel roleModel = JsonUtility.Parse<RoleModel>(CommonUtility.GetAjaxDataString());

            RolePositionDAM rolePositionDAM = new RolePositionDAM();

            if (roleModel.role_id > 0)
            {
                //修改职位
                result = rolePositionDAM.UpdatePositionInfo(roleModel);
            }
            else
            {
                //添加职位
                result = rolePositionDAM.SavePositionInfo(roleModel);

            }
            ajaxResult.result = result;

            if (result >= 0)
            {
                ajaxResult.message = "保存成功！";
            }
            else
            {
                ajaxResult.message = "保存出错！";
            }

            return Content(JsonUtility.ToJSON(ajaxResult));

        }
        #endregion
    }
}
